package com.gz.moreaseManager.modules.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gz.moreaseManager.config.SelfUserDetails;
import com.gz.moreaseManager.core.base.BaseController;
import com.gz.moreaseManager.core.modal.AjaxResult;
import com.gz.moreaseManager.core.modal.HttpCode;
import com.gz.moreaseManager.core.modal.PageResult;
import com.gz.moreaseManager.modules.entity.HelpType;
import com.gz.moreaseManager.modules.service.IHelpTypeService;
import com.gz.moreaseManager.utils.DateUtil;
import com.gz.moreaseManager.utils.UtilTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 帮助分类表 前端控制器
 * </p>
 *
 * @author wike
 * @since 2019-05-28
 */
@RestController
@RequestMapping("/help")
public class HelpTypeController extends BaseController {

    @Autowired
    private IHelpTypeService helpTypeService;

    @RequestMapping("list")
    public ModelAndView list(){
        return new ModelAndView("modules/helpType/list");
    }

    /**
     * 分页查询帮助分类信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("listPage")
    public PageResult<HelpType> findPageLsit(Integer pageNum, Integer pageSize){
        //分页
        Page paramsPage = new Page();
        paramsPage.setCurrent(pageNum);
        paramsPage.setSize(pageSize);
        //构建查询
        Wrapper wrapper = new EntityWrapper<HelpType>();
        //排序
        wrapper.orderBy("sort",true);
        wrapper.orderBy("create_time",false);
        Page<HelpType> data = helpTypeService.findHelpTypePage(paramsPage);
        return new PageResult(0, null, data.getTotal(), data.getRecords());

    }

    /**
     * 新增帮组分类
     * @param helpType
     * @return
     */
    @PostMapping("addHelpType")
    public AjaxResult<Boolean> addHelpType(HelpType helpType){
        if( helpType == null
                || UtilTool.isBlank( helpType.getHelpName())
                || UtilTool.isBlank( helpType.getSort()))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        helpType.setCreateTime(new Date());
        Boolean success = helpTypeService.insert(helpType);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "新增帮组分类信息失败!");
    }

    /**
     * 获取帮组分类信息
     * @param id
     * @return
     */
    @GetMapping("findHelpType")
    public AjaxResult<HelpType> findHelpType( Integer id ){
        if( UtilTool.isBlank( id ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        HelpType helpType = helpTypeService.selectById( id );
        return AjaxResult.success(helpType);
    }

    /**
     * 修改帮组分类
     * @param helpType
     * @return
     */
    @PostMapping("editHelpType")
    public AjaxResult<Boolean> editHelpType( HelpType helpType ){
        if( helpType == null
                || UtilTool.isBlank( helpType.getHelpId())
                || UtilTool.isBlank( helpType.getHelpName())
                || UtilTool.isBlank( helpType.getSort()))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        Boolean success = helpTypeService.insertOrUpdate(helpType);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "修改帮组分类信息失败!");
    }

    /**
     * 根据ID删除帮组分类信息
     * @param id
     * @return
     */
    @PostMapping("delete")
    public AjaxResult<Boolean> delete( Integer id){
        if( UtilTool.isBlank( id ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        Boolean success = helpTypeService.deleteById( id );
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "删除帮组分类信息失败!");
    }

    /**
     * 获取所有帮助分类信息
     * @return
     */
    @GetMapping("findHelpList")
    public AjaxResult<List<HelpType>> findHelpList(){
        List<HelpType> list = helpTypeService.selectList(null);
        if( UtilTool.isNotBlank( list ))
            return AjaxResult.success(list );
        return AjaxResult.build(HttpCode.FAILED, "获取帮组分类信息失败!");
    }
}
